﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Waiyun.WebApi
{
    public class Cache
    {
        private static List<string> _keys = new List<string>();
        private static List<DateTime> _timespan = new List<DateTime>();
        private static List<object> _values = new List<object>();
        private static object s_lock = new object();
        public static object Get(string key)
        {
            for (int index = 0; index < _keys.Count; index++)
            {
                if (string.Equals(_keys[index], key, StringComparison.CurrentCultureIgnoreCase))
                {
                    return _values[index];
                }
            }
            return null;
        }

        public static void Set(string key, object value)
        {
            lock (s_lock)
            {
                for (int index = 0; index < _timespan.Count; )
                {
                    if (_timespan[index].Date < DateTime.Now.Date)
                    {
                        // 删除这项，缓存时间已过期
                        _keys.RemoveAt(index);
                        _timespan.RemoveAt(index);
                        _values.RemoveAt(index);
                    }
                    else
                    {
                        index++;
                    }
                }

                for (int index = 0; index < _keys.Count; index++)
                {
                    if (string.Equals(_keys[index], key, StringComparison.CurrentCultureIgnoreCase))
                    {
                        return;
                    }
                }

                _keys.Add(key);
                _timespan.Add(DateTime.Now.Date);
                _values.Add(value);
            }
        }
    }
    public class Cache2
    {
        private static List<string> _keys = new List<string>();
        private static List<object> _values = new List<object>();
        private static object s_lock = new object();
        public static object Get(string key)
        {
            for (int index = 0; index < _keys.Count; index++)
            {
                if (string.Equals(_keys[index], key, StringComparison.CurrentCultureIgnoreCase))
                {
                    return _values[index];
                }
            }
            return null;
        }

        public static void Set(string key, object value)
        {
            lock (s_lock)
            {
                for (int index = 0; index < _keys.Count; index++)
                {
                    if (string.Equals(_keys[index], key, StringComparison.CurrentCultureIgnoreCase))
                    {
                        _values[index] = value;
                        Logger.InfoToTag("Cache2", string.Format("set name:{0}, value:{1}", key, value));
                        return;
                    }
                }

                _keys.Add(key);
                _values.Add(value);
            }
        }
    }
    public class ThreadPools
    {
        private static List<Thread> _values = new List<Thread>();
        private static object s_lock = new object();
        public static Thread Get(string name)
        {
            for (int index = 0; index < _values.Count; index++)
            {
                if (string.Equals(_values[index].Name, name, StringComparison.CurrentCultureIgnoreCase))
                {
                    return _values[index];
                }
            }
            return null;
        }
        public static void Start(Thread thread)
        {
            lock (s_lock)
            {

                for (int index = 0; index < _values.Count; index++)
                {
                    if (string.Equals(_values[index].Name, thread.Name, StringComparison.CurrentCultureIgnoreCase))
                    {
                        return;
                    }
                }
                _values.Add(thread);
                thread.Start();
            }
        }

        public static void Stop(string name)
        {
            lock (s_lock)
            {
                for (int index = 0; index < _values.Count; index++)
                {
                    if (string.Equals(_values[index].Name, name, StringComparison.CurrentCultureIgnoreCase))
                    {
                        _values.RemoveAt(index);
                        return;
                    }
                }
            }
        }
    }
}
